package org.monster.context.core;

import org.dom4j.DocumentException;
import org.monster.context.exception.XMLNotFoundException;
import org.monster.context.reader.WebXmlReader;
import org.monster.context.reader.XmlReader;
import org.monster.context.reader.tag.ServletTag;

import java.io.File;

/**
 * 配置服务
 *
 * @author Monster
 */
public class ConfigService {

    /**
     * xml阅读器
     */
    private final XmlReader<ServletTag> reader = new WebXmlReader<ServletTag>();

    /**
     * 解析配置
     * 读取web配置文件
     *
     * @param uriMapping uri映射
     * @param xmlPath    xml路径
     * @return 服务者类字符串
     * @throws XMLNotFoundException xml未找到异常
     * @throws DocumentException    文档异常
     */
    public String resolveConfig(String uriMapping, String xmlPath)
            throws XMLNotFoundException, DocumentException {
        ServletTag tag = null;
        if (uriMapping != null) {
            File file = new File(xmlPath);
            if (file.exists()) {
                reader.resolveElement(reader.getRootElement(file));
            }
            tag = reader.getTag(uriMapping);
        }
        if (tag != null) {
            return tag.getServletClass();
        }
        return null;
    }
}
